const Card = require("./Card");
const CardList = Card.CardList;
const CARD_LIST = (() => {
	const list = [];
	const types = "黑桃|红心|梅花|方块".split("|");
	const numbers = "1|2|3|4|5|6|7|8|9|10|J|Q|K".split("|");
	types.forEach(type => numbers.forEach(number => {
		const card = Card.buildIns(type, number);
		list.push(card);
	}));
	return list;
})();
// console.log(CARD_LIST.sort((a, b) => a - b).map(c => c.toString()).join())
class CardBox {
	constructor(props) {
		this.card_list = [];
		this.init()
	};
	init() {
		this.initCards();
		this.randomCards();
	};
	initCards() {
		this.card_list.length = 0;
		this.card_list.push(...CARD_LIST);
	};
	// 洗牌
	randomCards() {
		const old_list = this.card_list;
		const new_list = [];
		while (old_list.length > 0) {
			var random_index = (Math.random() * old_list.length) | 0;
			new_list.push(old_list[random_index]);
			old_list.splice(random_index, 1);
		}
		this.card_list = new_list;
	};
	// 抽牌
	removeCards(cardNum) {
		var arr = [];
		if (this.card_list.length >= cardNum) {
			arr.push(...this.card_list.splice(0, cardNum));
		}
		return arr;
	};
	moveCardsToPlayers(player_list, num = 13) {
		player_list.forEach(player => {
			this.moveCardsToPlayer(player, num)
		});
	};
	moveCardsToPlayer(player, num) {
		player.addCards(this.removeCards(num));
	};
}
module.exports = CardBox;
CardBox.CARD_LIST = CARD_LIST;